#pragma once

/**
 * Toolbar implementation.
 */
class CGooglePlusOneButtonToolbar : public CWindowImpl<CGooglePlusOneButtonToolbar, CToolBarCtrl>
{
public:
	CGooglePlusOneButtonToolbar(void);
	~CGooglePlusOneButtonToolbar(void);

	DECLARE_WND_SUPERCLASS(TEXT("GOOGLEPLUSONE"), CToolBarCtrl::GetWndClassName())

	BEGIN_MSG_MAP_EX(CGooglePlusOneButtonToolbar)
		COMMAND_ID_HANDLER_EX0(IDM_GOOGLEPLUSONE, OnButtonPresed)
		NOTIFY_RANGE_CODE_HANDLER_EX(IDM_GOOGLEPLUSONE, IDM_GOOGLEPLUSONE, TTN_GETDISPINFO, OnToolTip)
        MSG_WM_TIMER(OnTimer)
	END_MSG_MAP()

	bool Create(const CWindow &wndParent);

	void SetBrowser(IWebBrowser2* pBrowser);

	int GetMaxWidth();

    void BeforeNavigate2(IDispatch* pDisp, VARIANT* URL);
    void DocumentComplete(IDispatch* pDisp, VARIANT* URL);

protected: // Handlers
	LRESULT OnToolTip(LPNMHDR pnmh);

	void OnButtonPresed();
    void OnTimer(UINT_PTR nTimerId);

private:
	WEBBrowser	m_browser;
	WEBBrowser	m_browserLastGooglePlusOne;

    int         m_nIconIndex;
    CString     m_strLabel;
    CString     m_strTooltip;

    UINT_PTR    m_nTimerId;
    int         m_nTimerRepeatCount;

    bool IsApiScriptIncluded(const HTML::Document& doc);
    void InjectButton(HTML::Document& doc);
    HTML::Element FindChild(HTML::Element& elem, LPCWSTR pszTag);
    HTML::Element FindPlusOneButton();
    bool IsPressed(HTML::Element& elem);
    void DisableToolbarButton();
    void UpdateToolbarButton();
    void StartTimer();
    void StopTimer();
    void OpenShareDialog();

private:
	static CriticalSection	m_crsecInit;
	static bool				m_bInitialized;
	static TBBUTTON			m_buttons[1];
	static CManagedImageList m_image_list;

	static CString m_google_plus_one_api_url;
	static CString m_google_plus_one_frame_domain;
    static CStringW M_TRUE;
	static LPCWSTR DIV_ID;
    
	static void Init();

private:
	static CLogger logger;
};
